package com.ctsi.security.event.role;

import com.ctsi.commons.event.AbstractObserver;
import com.ctsi.commons.event.BaseEvent;
import com.ctsi.security.domain.RoleDataObject;
import com.ctsi.security.domain.RolePermission;
import com.ctsi.security.manager.impl.SecurityImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;


public class RemoveRoleCache extends AbstractObserver {
	/**
	 *
	 */
	private static final long serialVersionUID = 6374790178278758641L;
	private final Logger logger=LoggerFactory.getLogger(RemoveRoleCache.class);
	@Override
	public void onEvent(Object[] args) {
		BaseEvent event=(BaseEvent) args[0];
		Map<?,?> context=event.getContext();
		RoleDataObject roleDataObjectId=(RoleDataObject) context.get("roleDataObjectId");
		if(roleDataObjectId!=null){
			logger.info("清除角色{}缓存",roleDataObjectId.getRoleId());
			SecurityImpl.removeCacheRole(roleDataObjectId.getRoleId());
		}
		RolePermission rolePermissionId=(RolePermission)context.get("rolePermissionId");
		if(rolePermissionId!=null){
			logger.info("清除角色{}缓存",rolePermissionId.getRoleId());
			SecurityImpl.removeCacheRole(rolePermissionId.getRoleId());
		}
		String dataObjectType=(String) context.get("sourceDoTypeId");
		if(dataObjectType!=null){
			logger.info("清除数据对象类型{}的缓存",dataObjectType);
			SecurityImpl.removeCacheDoType(dataObjectType);
		}
	}

}
